﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDNhapKhoCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dechietkhau;
        private decimal _degiaban;
        private decimal _degiamua;
        private decimal _degianhap;
        private long _deid;
        private int _deidduocpham;
        private long _deidnhapkho;
        private decimal _desoluong;
        private decimal _desoluong1;
        private decimal _desoluong2;
        private decimal _desoluongxuat;
        private int _iiddangnhap;
        private int _iidvitrikho;
        private int _ithue;
        private string _shandung;
        private string _shangsx;
        private string _slo;
        private string _sngaycapnhat;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDNhapKhoCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDNhapKhoCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoNhapKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoNhapKhoCT.cs");
                return false;
            }
        }

        private int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoNhapKhoCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        private int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoNhapKhoCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoNhapKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            string cmdText = "";
            try
            {
                cmdText = string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDNHAPKHO, ",", columnIDDUOCPHAM, ",", columnSOLUONG, ",", columnSOLUONG1, ",", columnSOLUONG2, 
                    ",", columnGIAMUA, ",", columnTHUE, ",", columnGIABAN, ",", columnLO, ",", columnHANDUNG, ",", columnHANGSX, ",", columnTENMAYTINH, ",", columnIDDANGNHAP, 
                    ",", columnIDVITRIKHO, ",", columnSOLUONGXUAT, ",", columnCHIETKHAU, ",", columnGIANHAP, ") values(", this._deid, ",", this._deidnhapkho, ",", this._deidduocpham, ",:", columnSOLUONG, 
                    ",:", columnSOLUONG1, ",:", columnSOLUONG2, ",:", columnGIAMUA, ",", this._ithue, ",:", columnGIABAN, ",'", this._slo, "',to_timestamp('", this._shandung, "','dd/mm/yyyy'),:", columnHANGSX, 
                    ",:", columnTENMAYTINH, ",", this._iiddangnhap, ",", this._iidvitrikho, ",:", columnSOLUONGXUAT, ",:", columnCHIETKHAU, ",:", columnGIANHAP, ")"
                 });
                NpgsqlCommand command = new NpgsqlCommand(cmdText, connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHANGSX, NpgsqlDbType.Text, 200).Value = this._shangsx;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 15).Value = this._desoluong;
                command.Parameters.Add(columnSOLUONG1, NpgsqlDbType.Numeric, 15).Value = this._desoluong1;
                command.Parameters.Add(columnSOLUONG2, NpgsqlDbType.Numeric, 15).Value = this._desoluong2;
                command.Parameters.Add(columnGIAMUA, NpgsqlDbType.Numeric, 15).Value = this._degiamua;
                command.Parameters.Add(columnGIABAN, NpgsqlDbType.Numeric, 15).Value = this._degiaban;
                command.Parameters.Add(columnSOLUONGXUAT, NpgsqlDbType.Numeric, 15).Value = this._desoluongxuat;
                command.Parameters.Add(columnCHIETKHAU, NpgsqlDbType.Numeric, 15).Value = this._dechietkhau;
                command.Parameters.Add(columnGIANHAP, NpgsqlDbType.Numeric, 15).Value = this._degianhap;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(cmdText + exception.ToString(), "Insert()", "dtoNhapKhoCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidnhapkho = 0L;
            this._deidduocpham = 0;
            this._desoluong = 0M;
            this._desoluong1 = 0M;
            this._desoluong2 = 0M;
            this._degiamua = 0M;
            this._ithue = 0;
            this._degiaban = 0M;
            this._slo = "";
            this._shandung = "";
            this._shangsx = "";
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._iiddangnhap = 0;
            this._iidvitrikho = 0;
            this._desoluongxuat = 0M;
            this._dechietkhau = 0M;
            this._degianhap = 0M;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoNhapKhoCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDNHAPKHO, ",", columnIDDUOCPHAM, ",", columnSOLUONG, ",", columnSOLUONG1, ",", columnSOLUONG2, ",", columnGIAMUA, ",", columnTHUE, 
                    ",", columnGIABAN, ",", columnLO, ",to_char(", columnHANDUNG, ",'dd/mm/yyyy') as ", columnHANDUNG, ",", columnHANGSX, ",", columnTENMAYTINH, ",to_char(", columnNGAYCAPNHAT, ",'dd/mm/yyyy hh24:mi:ss') as ", columnNGAYCAPNHAT, 
                    ",", columnIDDANGNHAP, ",", columnIDVITRIKHO, ",", columnSOLUONGXUAT, ",", columnCHIETKHAU, ",", columnGIANHAP, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    "=", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidnhapkho = long.Parse(dataTable.Rows[0][columnIDNHAPKHO].ToString());
                    this._deidduocpham = int.Parse(dataTable.Rows[0][columnIDDUOCPHAM].ToString());
                    this._desoluong = decimal.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._desoluong1 = decimal.Parse(dataTable.Rows[0][columnSOLUONG1].ToString());
                    this._desoluong2 = decimal.Parse(dataTable.Rows[0][columnSOLUONG2].ToString());
                    this._degiamua = decimal.Parse(dataTable.Rows[0][columnGIAMUA].ToString());
                    this._ithue = int.Parse(dataTable.Rows[0][columnTHUE].ToString());
                    this._degiaban = decimal.Parse(dataTable.Rows[0][columnGIABAN].ToString());
                    this._slo = dataTable.Rows[0][columnLO].ToString();
                    this._shandung = dataTable.Rows[0][columnHANDUNG].ToString();
                    this._shangsx = dataTable.Rows[0][columnHANGSX].ToString();
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._iidvitrikho = int.Parse(dataTable.Rows[0][columnIDVITRIKHO].ToString());
                    this._desoluongxuat = decimal.Parse(dataTable.Rows[0][columnSOLUONGXUAT].ToString());
                    this._dechietkhau = decimal.Parse(dataTable.Rows[0][columnCHIETKHAU].ToString());
                    this._degianhap = decimal.Parse(dataTable.Rows[0][columnGIANHAP].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoNhapKhoCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoNhapKhoCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                string str = " 1=1 ";
                if (this._deidnhapkho != 0L)
                {
                    object obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnIDNHAPKHO, "=", this._deidnhapkho });
                }
                if (str != "")
                {
                    str = " 1=1 " + str;
                }
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDNHAPKHO + "," + columnIDDUOCPHAM + "," + columnSOLUONG + "," + columnSOLUONG1 + "," + columnSOLUONG2 + "," + columnGIAMUA + "," + columnTHUE + "," + columnGIABAN + "," + columnLO + ",to_char(" + columnHANDUNG + ",'dd/mm/yyyy') as " + columnHANDUNG + "," + columnHANGSX + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCAPNHAT + "," + columnIDDANGNHAP + "," + columnIDVITRIKHO + "," + columnSOLUONGXUAT + "," + columnCHIETKHAU + "," + columnGIANHAP + " from " + this._sSchema + "." + tableName + " where " + str, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoNhapKhoCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoNhapKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            string cmdText = "";
            try
            {
                cmdText = string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDNHAPKHO, " = ", this._deidnhapkho, ",", columnIDDUOCPHAM, " = ", this._deidduocpham, ",", columnSOLUONG, " = :", columnSOLUONG, 
                    ",", columnSOLUONG1, " = :", columnSOLUONG1, ",", columnSOLUONG2, " = :", columnSOLUONG2, ",", columnGIAMUA, " = :", columnGIAMUA, ",", columnTHUE, " = ", this._ithue, 
                    ",", columnGIABAN, " = :", columnGIABAN, ",", columnLO, " = '", this._slo, "',", columnHANDUNG, " = to_timestamp('", this._shandung, "','dd/mm/yyyy'),", columnHANGSX, " = :", columnHANGSX, 
                    ",", columnTENMAYTINH, " = :", columnTENMAYTINH, ",", columnNGAYCAPNHAT, " = now(),", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnIDVITRIKHO, " = ", this._iidvitrikho, ",", columnSOLUONGXUAT, 
                    " = :", columnSOLUONGXUAT, ",", columnCHIETKHAU, "=:", columnCHIETKHAU, ",", columnGIANHAP, "=:", columnGIANHAP, " where ", columnID, " = ", this._deid
                 });
                NpgsqlCommand command = new NpgsqlCommand(cmdText, connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHANGSX, NpgsqlDbType.Text, 200).Value = this._shangsx;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 15).Value = this._desoluong;
                command.Parameters.Add(columnSOLUONG1, NpgsqlDbType.Numeric, 15).Value = this._desoluong1;
                command.Parameters.Add(columnSOLUONG2, NpgsqlDbType.Numeric, 15).Value = this._desoluong2;
                command.Parameters.Add(columnGIAMUA, NpgsqlDbType.Numeric, 15).Value = this._degiamua;
                command.Parameters.Add(columnGIABAN, NpgsqlDbType.Numeric, 15).Value = this._degiaban;
                command.Parameters.Add(columnSOLUONGXUAT, NpgsqlDbType.Numeric, 15).Value = this._desoluongxuat;
                command.Parameters.Add(columnCHIETKHAU, NpgsqlDbType.Numeric, 15).Value = this._dechietkhau;
                command.Parameters.Add(columnGIANHAP, NpgsqlDbType.Numeric, 15).Value = this._degianhap;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(cmdText + exception.ToString(), "Update()", "dtoNhapKhoCT.cs");
                return false;
            }
        }

        public static string columnCHIETKHAU
        {
            get
            {
                return "chietkhau";
            }
        }

        public static string columnGIABAN
        {
            get
            {
                return "giaban";
            }
        }

        public static string columnGIAMUA
        {
            get
            {
                return "giamua";
            }
        }

        public static string columnGIANHAP
        {
            get
            {
                return "gianhap";
            }
        }

        public static string columnHANDUNG
        {
            get
            {
                return "handung";
            }
        }

        public static string columnHANGSX
        {
            get
            {
                return "hangsx";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDUOCPHAM
        {
            get
            {
                return "idduocpham";
            }
        }

        public static string columnIDNHAPKHO
        {
            get
            {
                return "idnhapkho";
            }
        }

        public static string columnIDVITRIKHO
        {
            get
            {
                return "idvitrikho";
            }
        }

        public static string columnLO
        {
            get
            {
                return "lo";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnSOLUONG1
        {
            get
            {
                return "soluong1";
            }
        }

        public static string columnSOLUONG2
        {
            get
            {
                return "soluong2";
            }
        }

        public static string columnSOLUONGXUAT
        {
            get
            {
                return "soluongxuat";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public static string columnTHUE
        {
            get
            {
                return "thue";
            }
        }

        public string NGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public decimal pCHIETKHAU
        {
            get
            {
                return this._dechietkhau;
            }
            set
            {
                this._dechietkhau = value;
            }
        }

        public decimal pGIABAN
        {
            get
            {
                return this._degiaban;
            }
            set
            {
                this._degiaban = value;
            }
        }

        public decimal pGIAMUA
        {
            get
            {
                return this._degiamua;
            }
            set
            {
                this._degiamua = value;
            }
        }

        public decimal pGIANHAP
        {
            get
            {
                return this._degianhap;
            }
            set
            {
                this._degianhap = value;
            }
        }

        public string pHANDUNG
        {
            get
            {
                return this._shandung;
            }
            set
            {
                this._shandung = value;
            }
        }

        public string pHANGSX
        {
            get
            {
                return this._shangsx;
            }
            set
            {
                this._shangsx = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDDUOCPHAM
        {
            get
            {
                return this._deidduocpham;
            }
            set
            {
                this._deidduocpham = value;
            }
        }

        public long pIDNHAPKHO
        {
            get
            {
                return this._deidnhapkho;
            }
            set
            {
                this._deidnhapkho = value;
            }
        }

        public int pIDVITRIKHO
        {
            get
            {
                return this._iidvitrikho;
            }
            set
            {
                this._iidvitrikho = value;
            }
        }

        public string pLOSX
        {
            get
            {
                return this._slo;
            }
            set
            {
                this._slo = value;
            }
        }

        public decimal pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public decimal pSOLUONG1
        {
            get
            {
                return this._desoluong1;
            }
            set
            {
                this._desoluong1 = value;
            }
        }

        public decimal pSOLUONG2
        {
            get
            {
                return this._desoluong2;
            }
            set
            {
                this._desoluong2 = value;
            }
        }

        public decimal pSOLUONGXUAT
        {
            get
            {
                return this._desoluongxuat;
            }
            set
            {
                this._desoluongxuat = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public int pTHUE
        {
            get
            {
                return this._ithue;
            }
            set
            {
                this._ithue = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_nhapkhoct";
            }
        }
    }
}

